<template>
  <view class="setting-container">
    <view class="setting-view u-m-b-20">
      <view class="u-flex u-m-t-20 u-m-b-20">
        <view class="u-flex u-flex-1" @click="handleJudgeLogin('/mine-module/profile')">
          <u-image width="100" height="100" shape="circle" :src="user.face || defaultFace"></u-image>
          <view class="u-m-l-20" v-if="user">
            <view class="">
              {{ user.nickname}}
            </view>
            <view class="uname">
              用户名：{{ user.uname}}
            </view>
          </view>
          <view class="u-m-l-20" v-else>登录/注册</view>
        </view>
        <view class="">
          <u-icon name="arrow-right" color="#969799"></u-icon>
        </view>
      </view>
      <u-line color="#F6F7F9" />
      <view class="u-flex u-m-t-30 u-m-b-10" @click="handleJudgeLogin('/mine-module/address')">
        <view class="u-flex-1">
          地址管理
        </view>
        <view class="">
          <u-icon name="arrow-right" color="#969799"></u-icon>
        </view>
      </view>
    </view>
    <view class="setting-cont u-m-b-20" v-if="!identity">
      <u-cell-group :border="false">
        <u-cell-item title="实名认证" @click="handleJudgeLogin('/pages/auth/authentication', 1)"></u-cell-item>
      </u-cell-group>
    </view>
    <view class="setting-cont u-m-b-20" v-if="!identity">
      <u-cell-group :border="false">
        <u-cell-item title="人工审核验证" @click="handleJudgeLogin('/pages/auth/authentication2', 1)"></u-cell-item>
      </u-cell-group>
    </view>
    <view class="setting-cont u-m-b-20">
      <u-cell-group :border="false">
        <u-cell-item title="账户安全" @click="handleJudgeLogin('/mine-module/account-safe')"></u-cell-item>
      </u-cell-group>
    </view>
    <!-- <view class="setting-cont u-m-b-20" v-if="res.corporate">
      <u-cell-group :border="false">
        <u-cell-item title="合作商收款" :arrow="false">
          <view class="u-f-ac" slot="right-icon">
            <view style="width: 250rpx;margin-right: 30rpx;">
              <u-input type="number" v-model="amount" placeholder="请输入收款金额"></u-input>
            </view>
            <u-icon name="scan" size="50" @click="scanClick"></u-icon>
          </view>
        </u-cell-item>
      </u-cell-group>
    </view> -->
    <view class="logout-view u-m-t-20" v-if="user" @click="onLogout">
      <view class="">
        退出登录
      </view>
    </view>

    <u-modal title="等待收款中" :content="content" v-model="showPopup"></u-modal>
  </view>
</template>
<script>
  var timer;
  import * as API_Member from '@/api/members.js';
  import * as API_Members from '@/api/members.js'
  import Cache, {
    Keys
  } from '@/utils/cache.js';

  export default {
    data() {
      return {
        user: '',
        defaultFace: '/static/images/member/icon-noface.jpg',
        identity: false,
        res: {},

        amount: '',
        time: 0,
        showPopup: false,
        content: '',
      };
    },
    onShow() {
      this.memberInfo()
      this.user = Cache.getItem('user')
    },
    onUnload() {
      console.log('onUnload')
      clearInterval(timer)
      this.time = 0
    },
    onHide() {
      console.log('onHide')
      clearInterval(timer)
      this.time = 0
    },
    methods: {
      memberSignal() {
        timer = setInterval(async () => {
          this.time += 1
          if (this.time >= 60) {
            clearInterval(timer)
          }
          let res = await API_Members.memberSignal()
          console.log('查看商家方轮询', this.time, res)
          if (res) {
            let msg = res.pay_amount.toString().substr(0, 1) == '-' ? '收款' : '付款'
            this.content = `${msg}${res.pay_amount}元成功`
            setTimeout(() => {
              this.showPopup = false
              clearInterval(timer)
              this.time = 0
              this.amount = ''
            }, 2000)
          }
        }, 1000)
      },
      async memberInfo() {
        let res = await API_Members.memberInfo()
        console.log(res)
        this.res = res
        this.identity = res.info.identity
      },
      //点击事件
      handleJudgeLogin(url, value) {
        if (value && this.identity) {
          return this.$u.toast('已实名注册')
        }
        console.log(url, value)
        this.judgeLogin(() => {
          uni.navigateTo({
            url: url
          })
        }, 'force')
      },
      scanClick() {
        let that = this
        if (!this.amount) return this.$u.toast('请输入收款金额')

        // #ifdef  APP-PLUS
        // 允许从相机和相册扫码
        uni.scanCode({
          onlyFromCamera: true,
          scanType: ['qrCode', 'barCode'],
          // scanType: ['UPC_E'],
          success: function(res) {
            console.log('条码内容：' + JSON.stringify(res))
            console.log(typeof res.result)
            API_Members.scanPayCode({
              amount: that.amount,
              code: res.result
            }).then(response => {
              console.log(response)
              that.content = `请耐心等待对方确认该交易，${response}`
              clearInterval(timer)
              that.time = 0
              that.memberSignal()
              that.showPopup = true
              // that.$u.toast(`收款${that.amount}元成功`)
              // that.amount = ''
            }).catch(err => {
              console.log(err)
            })
          }
        })
        // #endif

      },

      //退出登录
      onLogout() {
        uni.$emit('logout', {
          msg: '退出登录消息'
        })
        //清除会员信息
        uni.removeStorageSync(Keys.user);
        //清除token
        uni.removeStorageSync(Keys.accessToken);
        //后退并刷新数据
        uni.navigateBack({
          delta: 1,
          animationDuration: 300
        })
      }
    }
  }
</script>

<style lang="scss">
  .setting-view {
    background-color: #FFFFFF;
    padding: 26rpx 32rpx;
    border-bottom-left-radius: 20rpx;
    border-bottom-right-radius: 20rpx;

    .uname {
      font-size: 22rpx;
      color: #909399;
      margin-top: 10rpx;
    }
  }

  .setting-cont {
    background-color: #FFFFFF;
    border-radius: 20rpx;
    overflow: hidden;

    /deep/.u-cell_title {
      color: #333333;
    }
  }

  .logout-view {
    border-radius: 20rpx;
    background-color: #FFFFFF;
    padding: 20rpx;
    text-align: center;
  }
</style>
